/**
 * Copyright 2014 Michail Basios
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * This class generates new source files for ArrayList collection according to
 * the Object being stored.
 * 
 * @author <a href="http://www.basios.com" target="blank">Michail Basios</a>
 * @version 1.0 Build February 17, 2014.
 * 
 */
public class TemplateGenerator {

	
	private URL url;
	
	private String templateArrayListFile;
	
	private String workingDir;
	
	private Path pathArraylist;
	
	private String contentArraylist;
	
	/**
	 * Generates the new class source file which will extend the collection by
	 * adding indexes Example: ArrayList{@code <Person>} PersonArrayList
	 * 
	 * @param className
	 *            The name of the class to be generated
	 * 
	 * @param keyName
	 *            This will contain the name of the key as declared by the user
	 *            (either with annotation or with something else that we dont
	 *            know yet) For example: Person
	 * 
	 * @param accessName
	 *            This is used in case a variable is private and accessed
	 *            through a getter method
	 * 
	 * 
	 * 
	 * @param keyObject
	 *            Defines what type of Object the key is. For example: Integer,
	 *            Double, String
	 * 
	 * @param keyPrimitive
	 *            defines the kind of primitive type the project is (This can be
	 *            eliminated probably by using a HashMap which contain the
	 *            proper primitive types for the each Object)
	 */

	public void generateArrayList(String className, String keyName,
			String accessName, String keyObject, String keyPrimitive) {

		/*
		 * This is the input template file that should be filled with the proper
		 * specialised fields.
		 */

		url = TemplateGenerator.class.getResource("resources/");
		//System.out.println("Ta resources einai: " + url);
		//System.out.println(url.getPath());

		templateArrayListFile = "TabularTemplate.txt";

		workingDir = System.getProperty("user.dir");
		//System.out.println("The working directory: " + workingDir);

		// Path pathArraylist = Paths.get(workingDir + "//"
		// + templateArrayListFile);

		 pathArraylist = Paths.get(url.getPath() + "//"
				+ templateArrayListFile);

	//	System.out.println("To path gia to arrayList is: " + pathArraylist);
		Charset charset = StandardCharsets.UTF_8;
		contentArraylist = null;
		try {
			contentArraylist = new String(Files.readAllBytes(pathArraylist),
					charset);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// System.out.println(contentArraylist);

		/*
		 * This block of code contains the proper replacement that should happen
		 * in the template file in order to have a correct Java Source file
		 */
		contentArraylist = contentArraylist.replaceAll("#E#", className);
		contentArraylist = contentArraylist.replaceAll("#KeyName#", keyName);
		contentArraylist = contentArraylist.replaceAll("#AccessName#",
				accessName);
		contentArraylist = contentArraylist
				.replaceAll("#KeyObject#", keyObject);
		contentArraylist = contentArraylist.replaceAll("#KeyPrimitive#",
				keyPrimitive);
		
		

		// System.out.println(contentArraylist);
		
		/*
		 * Save the new specialised collection with new name
		 */
		templateArrayListFile = "temp/"+className + "Tabular.java";
		//System.out.println("Apothikevetai sto: "+templateArrayListFile);

		// pathArraylist = Paths.get("//home//maba18//Desktop//templates//"
		// + templateArrayListFile);

		//System.out.println(workingDir);
		
		File dir = new File(new File(Main.folderPath).toString()
				+ "/temp");
	//	System.out.println("The folder path for the input project is: " + dir);
		if (!dir.exists()) {

			dir.mkdir();
		}
		
		pathArraylist = Paths.get(Main.folderPath + "//"
				+ templateArrayListFile);

		saveFile();
		
		


	}
	
	public void generateUniqueIndexes(String indexDeclaration, String indexInstantiation, String indexesGetters, String AddFunction){
		
		contentArraylist = contentArraylist.replaceAll("//#otherIndexes#", indexDeclaration);
		
		System.out.println("Mesa: "+indexInstantiation);
		contentArraylist = contentArraylist.replaceAll("//#IndexesInstatiation#", indexInstantiation);
		contentArraylist = contentArraylist.replaceAll("//#IndexesGetters#", indexesGetters);
		contentArraylist = contentArraylist.replaceAll("//#addFunction#", AddFunction);
		
		
		saveFile();
	//	contentArraylist = contentArraylist.replaceAll("#KeyName#", indexInstantiation);
	}
	
	public void saveFile(){
		
		//System.out.println("Mike" + pathArraylist);
		try {
			Files.write(pathArraylist, contentArraylist.getBytes(StandardCharsets.UTF_8));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	public String getContentArraylist() {
		return contentArraylist;
	}

	public void setContentArraylist(String contentArraylist) {
		this.contentArraylist = contentArraylist;
	}
	
	
	

}
